package com.baidu.tracks.mylogin;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.tracks.R;


public class SettingActivity extends AppCompatActivity {

    private EditText sexEditText; // 学号编辑框
    private EditText phoneEditText;
    private EditText userEditText;
    private EditText userName;// 姓名编辑框
    private Button modifyButton; // 修改按钮
    private Button saveButton; // 保存按钮

    private String currentsex; // 当前学号
    private String currentphone;
    private String currentuser;
    private String currentuserName;
    // 当前姓名
    private boolean isEditable = false; // 编辑框是否可编辑的标志

    private SharedPreferences sharedPreferences; // SharedPreferences对象，用于存储数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Button btn_logoff = findViewById(R.id.logoff);
        btn_logoff.setOnClickListener(view -> {
            Intent intent = new Intent(SettingActivity.this, Login.class);
            startActivity(intent);
            finish();
        });
        // 初始化视图组件
        sexEditText = findViewById(R.id.sexEditText);
        phoneEditText = findViewById(R.id.phoneEditText);
        userEditText = findViewById(R.id.userEditText);
        userName = findViewById(R.id.userName);
        modifyButton = findViewById(R.id.modifyButton);
        saveButton = findViewById(R.id.saveButton);

        // 获取SharedPreferences实例
        sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);

        // 修改按钮的点击监听器
        modifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isEditable = true;
                sexEditText.setEnabled(true);
                phoneEditText.setEnabled(true);
                userEditText.setEnabled(true);
                userName.setEnabled(true);
            }
        });

        // 保存按钮的点击监听器
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isEditable = false;
                currentsex = sexEditText.getText().toString();
                currentphone = phoneEditText.getText().toString();
                currentuser = userEditText.getText().toString();
                currentuserName = userName.getText().toString();
                sexEditText.setEnabled(false);
                phoneEditText.setEnabled(false);
                userEditText.setEnabled(false);
                userName.setEnabled(false);
                // 保存编辑框的内容到SharedPreferences
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("id", currentsex);
                editor.putString("name", currentphone);
                editor.putString("name", currentuser);
                editor.putString("name", currentuserName);
                editor.apply();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isEditable) {
            sexEditText.setEnabled(false);
            phoneEditText.setEnabled(false);
            userEditText.setEnabled(false);
            userName.setEnabled(false);
            // 从SharedPreferences加载编辑框的内容
            currentsex = sharedPreferences.getString("id", "");
            currentphone = sharedPreferences.getString("name", "");
            currentuser = sharedPreferences.getString("name", "");
            currentuserName = sharedPreferences.getString("name", "");
            sexEditText.setText(currentsex);
            phoneEditText.setText(currentphone);
            userEditText.setText(currentuser);
            userName.setText(currentuser);
        }
    }
}


